//
//  MVRootViewController.m
//  SunXi
//
//  Created by dllo on 15/12/3.
//  Copyright © 2015年 大远. All rights reserved.
//

#import "MVRootViewController.h"

@interface MVRootViewController ()<UITableViewDataSource, UITableViewDelegate>
@property(nonatomic, retain) UITableView *tableView;
@property(nonatomic, retain) NSMutableArray *arr;
@property(nonatomic, assign) NSInteger page;
@end

@implementation MVRootViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.page = 1;
    self.arr = [NSMutableArray array];
    SV.BC = [UIColor whiteColor];
    self.navigationItem.title = @"每日视频";
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 64) style:UITableViewStylePlain];
    [SV addSubview:self.tableView];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.separatorStyle = NO;
    [ShowMytext showHUDloadView:SV];
    [self create];
    
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.page = 1;
        [self create];
        
        [self.tableView reloadData];
    }];
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        if (self.page <= 2) {
            self.page++;
            
            [self create];
        }
      }];
    
}
//MARK:数据解析
- (void)create {
    NSString *url = @"http://magicapi.vmovier.com/magicapi/find";
    
    NSString *para = [NSString stringWithFormat:@"json=1&p=%ld",self.page];
    [DyAFnetWorking cachePostWithURL:url WithbodyStr:para success:^(id result) {
        
        self.arr = [MVmodel baseMedelByArr:self.arr addArr:[MVmodel baseModelByArr:result[@"data"]]];
        
        [self.tableView reloadData];
        [ShowMytext hiddHUDloadView];
        [self.tableView.mj_footer endRefreshing];
        [self.tableView.mj_header endRefreshing];

        
    } failure:^(id nonet) {
        
        
    }];
}

- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.arr.count;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return WIDTH * 388 / 692;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *reuse = @"reuse";
    MVTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
    
    if (!cell) {
        cell = [[MVTableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuse];
    }
    
    MVmodel *model = self.arr[indexPath.row];
    cell.model = model;
    
    return cell;
    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    MVDetailsViewController *MVDVC = [[MVDetailsViewController alloc] init];
    
    MVDVC.str = [self.arr[indexPath.row] videolink];
    MVDVC.mytitle = [self.arr[indexPath.row] title];
    [self.navigationController pushViewController:MVDVC animated:YES];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
